package foodtable;

import java.util.List;

import com.andro.chansung.R;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.TextView;

public class DormFoodAdapter extends BaseAdapter {
	
	private List<FoodList> foodLists;
	private LayoutInflater inflater;
	private Context context;
	private ViewHolder viewHolder;

	private class ViewHolder {
		public TextView timeTv  = null;
		public TextView kindATv = null;
		public TextView kindBTv = null;
		
		public TextView kindATitleTv = null;
		public TextView kindBTitleTv = null;
	}

	public DormFoodAdapter(Context context, List<FoodList> foodLists) {
		this.context = context;
		this.foodLists = foodLists;
		this.inflater = LayoutInflater.from(context);
	}
	
	public int getCount() {
		return foodLists.size();
	}

	public FoodList getItem(int position) {
		return foodLists.get(position);
	}

	public long getItemId(int position) {
		return position;
	}

	public View getView(int position, View convertView, ViewGroup parent) {
		View view = convertView;
		
		if( view == null ) {
			viewHolder = new ViewHolder();
			view = inflater.inflate(R.layout.dorm_food_list_view_cell_layout, null);
			viewHolder.timeTv	= (TextView)view.findViewById(R.id.dorm_food_list_view_cell_time_tv);
			viewHolder.kindATv 	= (TextView)view.findViewById(R.id.dorm_food_list_view_cell_typea_tv);
			viewHolder.kindBTv 	= (TextView)view.findViewById(R.id.dorm_food_list_view_cell_typeb_tv);
			viewHolder.kindATitleTv = (TextView)view.findViewById(R.id.dorm_food_list_view_cell_typea_title_tv);
			viewHolder.kindBTitleTv = (TextView)view.findViewById(R.id.dorm_food_list_view_cell_typeb_title_tv);
			view.setTag(viewHolder);
		} else {
			viewHolder = (ViewHolder)view.getTag();
		}
		
		FoodList foodList = getItem(position);
		List<Food> foods = foodList.getFoods();
		viewHolder.timeTv.setText(foods.get(0).getTime().toString());
		
		String menuStr = foods.get(0).getMenu();
		viewHolder.kindATv.setText(menuStr);
		
		if( foods.size() <= 1 ) {
			LinearLayout layout = (LinearLayout)view.findViewById(R.id.dorm_food_kindb_container);
			layout.setVisibility(View.GONE);
		} else {
			viewHolder.kindBTitleTv.setText("간편식");
			viewHolder.kindBTv.setText(foods.get(1).getMenu());
		}
		
//		if( menuStr.contains("<간편식>") ) {
//			layout.setVisibility(View.VISIBLE);
//			String normal = menuStr.substring(menuStr.indexOf("<정식>") + 4, menuStr.lastIndexOf("<간편식>"));
//			String simple = menuStr.substring(menuStr.indexOf("<간편식>") + 5);
//			
//			viewHolder.kindBTitleTv.setText("간편식");
//			viewHolder.kindATv.setText(normal);
//			viewHolder.kindBTv.setText(simple);
//		} else if ( menuStr.contains("<일품>")) {
//			String normal = menuStr.substring(menuStr.indexOf("<정식>") + 4, menuStr.indexOf("<일품>"));
//			String simple = menuStr.substring(menuStr.indexOf("<일품>") + 4);
//			
//			viewHolder.kindBTitleTv.setText("일품");
//			viewHolder.kindATv.setText(normal);
//			viewHolder.kindBTv.setText(simple);
//		} else {
//			if( menuStr.contains("<분식데이>") ) {
//				viewHolder.kindATitleTv.setText("분식데이");
//				menuStr = menuStr.substring(menuStr.indexOf("<분식데이>") + 6);
//			} else if ( menuStr.contains("<정식>") ) {
//				menuStr = menuStr.substring(menuStr.indexOf("<정식>") + 4);
//			}
//			layout.setVisibility(View.INVISIBLE);
//			viewHolder.kindATv.setText(menuStr);
//		}
		
		return view;
	}
}
